<?php

namespace app\modules\user\controllers;

use yii;
use yii\web\Controller;
use common\models\Tools;
use common\models\Works;
use common\models\WorksRead;
use common\models\WorksComment;
use common\models\WorksCollection;
/**
 * 作者主页
 */
class HomeController extends Controller
{
    public $enableCsrfValidation = false;
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        $autherid = Tools::GetSafeParam('id', 0, 0);
        //是否显示关注按钮
        $is_self = 0;
        $is_interested = 0;
        if(!Yii::$app->user->isGuest){
            $is_self = $autherid == Yii::$app->user->identity->id ? 1:0;
            $is_interested = \common\models\UserInterest::checkIsInterested(Yii::$app->user->identity->id, $autherid);
        }
        
        $user = \common\models\User::getUserInfo($autherid);
        $works = Works::getUserWorks($autherid);
        //作品数量
        $works_num = Works::getUserWorksNum($autherid);
        //粉丝数量
        $fan_num = \common\models\UserInterest::getFanNum($autherid);
        
        foreach($works as $key=>$work){
            //$works[$key]['commentnum'] = WorksComment::getWorksCommentNum($work['id']);
            $works[$key]['comment_num'] = WorksComment::getWorksCommentNum($work['id']);
            $works[$key]['collect_num'] = WorksComment::getWorksCollectNum($work['id']);
            
            $works[$key]['is_collection'] = Yii::$app->user->isGuest ? 0: WorksCollection::checkExist(Yii::$app->user->identity->id,$work['id']);
            $works[$key]['addtime'] = Tools::calculateTimelong($work['addtime']);
        }
        $this->getView()->title = $user['username']."的主页";
        //$this->layout = "/chat";
        return $this->render('index',['works'=>$works,'user'=>$user,'is_self'=>$is_self,'is_interested'=>$is_interested,'works_num'=>$works_num,'fan_num'=>$fan_num]);
    }
    
}
